import {Modal, Button,Icon,Row,Col,Input} from 'antd';
import * as API_LIST from '../apis/fnaSpecial'
const InputGroup = Input.Group;
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
const {TableStore} = WeaTableNew
import {WeaDialog} from 'ecCom';
import trim from 'lodash/trim';

export default class PopupBillNumberList extends React.Component{

	constructor(props) {
		super(props);
		this.state = { visible: false,value:"",tableStore:new TableStore() };
    }

    componentDidMount(){
        const {fieldSet,requestid,fieldGet,rowIndex,fieldId,detailList,detailType} = this.props;
        let detailKey = WfForm.getDetailRowKey("field"+fieldId+"_"+rowIndex);//获取明细表的主键

        if(detailList.length > 0 && requestid != "-1"){
            if(detailList[0]){
                if(detailList[0][detailKey]){
                    this.setState({
                        value: detailList[0][detailKey].dnxhShowName
                    });
                }
            }
        }   

        //初始化明细后替换原明细数据
        if(detailType == 'borrow'){
            let jklcReid =  WfForm.getFieldValue("field"+fieldGet[0]+"_"+rowIndex);
            let dnxh =  WfForm.getFieldValue("field"+fieldId+"_"+rowIndex);
            let mapkey = "borrowRequestIdInfo_"+jklcReid+"_"+dnxh;
            if(detailList[0]){
                if(detailList[0][mapkey]){
                    WfForm.changeFieldValue("field"+fieldSet[1]+"_"+rowIndex, {value: detailList[0][mapkey].jkje}); //借款金额
                    WfForm.changeFieldValue("field"+fieldSet[2]+"_"+rowIndex, {value: detailList[0][mapkey].yhje});//已还金额
                    WfForm.changeFieldValue("field"+fieldSet[3]+"_"+rowIndex, {value: detailList[0][mapkey].spzje});//审批中待还金额
                    WfForm.changeFieldValue("field"+fieldSet[4]+"_"+rowIndex, {value: detailList[0][mapkey].whje});//未还金额
                }
            }          
        }

        if(detailType == 'advance'){
            let YfklcReid =  WfForm.getFieldValue("field"+fieldGet[0]+"_"+rowIndex);
            let dnxh =  WfForm.getFieldValue("field"+fieldId+"_"+rowIndex);
            let mapkey = "advanceRequestIdInfo_"+YfklcReid+"_"+dnxh;
            if(detailList[0]){
                if(detailList[0][mapkey]){
                    WfForm.changeFieldValue("field"+fieldSet[1]+"_"+rowIndex, {value: detailList[0][mapkey].Yfkje}); //预付款金额
                    WfForm.changeFieldValue("field"+fieldSet[2]+"_"+rowIndex, {value: detailList[0][mapkey].yhje});//已还金额
                    WfForm.changeFieldValue("field"+fieldSet[3]+"_"+rowIndex, {value: detailList[0][mapkey].spzje});//审批中待还金额
                    WfForm.changeFieldValue("field"+fieldSet[4]+"_"+rowIndex, {value: detailList[0][mapkey].whje});//未还金额
                }
            }        
        }  
        
    }

    showModal = () => {
	    this.setState({
	    	visible: true,
	    });

        const {requestid,fieldGet,rowIndex,detailType} = this.props;
        let lcId = WfForm.getFieldValue("field"+fieldGet[0]+"_"+rowIndex);

        if(detailType == 'borrow'){
            API_LIST.queryDtlRowData_borrow({'requestid':requestid,'jklc':lcId}).then(data =>{
                let tableStoreTemp = new TableStore();
                tableStoreTemp.getDatas(data.datas,1);
    
                this.setState({
                    tableStore:tableStoreTemp
                });
            });    
        }else if(detailType == 'advance'){
            API_LIST.queryDtlRowData_advance({'requestid':requestid,'Yfklc':lcId}).then(data =>{
                let tableStoreTemp = new TableStore();
                tableStoreTemp.getDatas(data.datas,1);
    
                this.setState({
                    tableStore:tableStoreTemp
                });
            });   
        }

	}

	handleCancel = (e) => {
    	this.setState({
      		visible: false,
    	});
    }

    rowClick = (record, index) => {
        const {fieldSet,requestid,fieldGet,rowIndex,fieldId,detailType} = this.props;
        let lcId = WfForm.getFieldValue("field"+fieldGet[0]+"_"+rowIndex);
        let applicant = WfForm.getFieldValue("field"+fieldGet[1]);//申请人
        let dnxh = record.id;
        let typeId = record.typeId;//借款类型 

        if(detailType == 'borrow'){
            API_LIST.borrowInfoData({'requestid':requestid,'jklc':lcId,'dnxh':dnxh,'typeId':typeId,'applicant':applicant}).then(data =>{
                if(data.flag == true){
                    WfForm.changeFieldValue("field"+fieldSet[0]+"_"+rowIndex, {value: data.requestmark}); //借款单号
                    WfForm.changeFieldValue("field"+fieldSet[1]+"_"+rowIndex, {value: data.jkje}); //借款金额
                    WfForm.changeFieldValue("field"+fieldSet[2]+"_"+rowIndex, {value: data.yhje});//已还金额
                    WfForm.changeFieldValue("field"+fieldSet[3]+"_"+rowIndex, {value: data.spzje});//审批中待还金额
                    WfForm.changeFieldValue("field"+fieldSet[4]+"_"+rowIndex, {value: data.whje});//未还金额
    
                    WfForm.changeFieldValue("field"+fieldId+"_"+rowIndex, {value: dnxh});//单内序号
                    this.setState({
                        visible: false,
                        value:data.dnxhShowName
                    });
                }
            });
        }else if(detailType == 'advance'){
            API_LIST.advanceInfoData({'requestid':requestid,'Yfklc':lcId,'dnxh':dnxh,'typeId':typeId}).then(data =>{
                if(data.flag == true){
                    WfForm.changeFieldValue("field"+fieldSet[0]+"_"+rowIndex, {value: data.requestmark}); //借款单号
                    WfForm.changeFieldValue("field"+fieldSet[1]+"_"+rowIndex, {value: data.Yfkje}); //预付款金额
                    WfForm.changeFieldValue("field"+fieldSet[2]+"_"+rowIndex, {value: data.yhje});//已还金额
                    WfForm.changeFieldValue("field"+fieldSet[3]+"_"+rowIndex, {value: data.spzje});//审批中待还金额
                    WfForm.changeFieldValue("field"+fieldSet[4]+"_"+rowIndex, {value: data.whje});//未还金额
    
                    WfForm.changeFieldValue("field"+fieldId+"_"+rowIndex, {value: dnxh});//单内序号
                    this.setState({
                        visible: false,
                        value:data.dnxhShowName
                    });
                }
            });
        }   

    }

  	render(){
        const {tableStore} = this.state;
        const {nodeType,detailType,viewAttr,fieldId,rowIndex,value} = this.props;
        let title = '';
        if(detailType == 'borrow'){
            title = '借款明细';
        }else if(detailType == 'advance'){
            title = '预付款明细';
        }

        const isRequired = viewAttr === 3 && trim(value).length === 0;
        let fieldName = 'field'+fieldId+'_'+rowIndex;
        
  		return(
  			<div className="wf-form-input">
  				<InputGroup ecId={`${this && this.props && this.props.ecId || ''}_InputGroup@54w94c`} >
                {/* {this.state.value} */}
                <span>
                    <span id={`${fieldName}span`}  dangerouslySetInnerHTML={{__html:value}}></span>
					<input type="hidden" id={fieldName} name={fieldName} value={value}/>
				</span>
                <div className="ant-input-group-wrap" style={viewAttr ==1 && typeof(nodeType) !='undefined'?{display:'none'}:{}}>
                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2gi25e`} icon="search"  onClick={this.showModal} />
                    <span style={{right: 2}} className="wf-input-required">
                        {isRequired ? <img src="/images/BacoError_wev9.png" align="absmiddle" /> : ""}
                    </span>
                </div>
		        </InputGroup>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@aoz03v`}
                    title={title}
                    icon = 'icon-coms-Finance'
                    iconBgcolor = '#f14a2d'
                    maskClosable={false}
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    buttons={[
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5dh3si@Cancel`} key="back" type="ghost" size="large" onClick={this.handleCancel} >取 消</Button>,
                    ]}
                >
                    <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@4u5859`} 
                        hasOrder={true}
                        needScroll={true}
                        comsWeaTableStore={tableStore}
                        onRowClick={this.rowClick}
                    />
                </WeaDialog>

  			</div>	
  		);
  	}	
}